package com.app.biz.service.impl;

import java.util.*;
import java.util.stream.Collectors;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.app.biz.domain.request.BizManufacturingPmiComparisonRequest;
import com.app.biz.domain.BizManufacturingPmiComparison;
import com.app.biz.utils.MaxMinUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.app.biz.mapper.BizManufacturingPmiComparisonFormalMapper;
import com.app.biz.domain.formal.BizManufacturingPmiComparisonFormal;
import com.app.biz.service.IBizManufacturingPmiComparisonFormalService;

/**
 * 山东与全国制造业采购经理指数对比正式Service业务层处理
 *
 * @author SangGuangCan
 * @date 2024-01-02
 */
@Service
public class BizManufacturingPmiComparisonFormalServiceImpl implements IBizManufacturingPmiComparisonFormalService
{
    @Autowired
    private BizManufacturingPmiComparisonFormalMapper bizManufacturingPmiComparisonFormalMapper;

    /**
     * 查询山东与全国制造业采购经理指数对比正式
     *
     * @param baseId 山东与全国制造业采购经理指数对比正式主键
     * @return 山东与全国制造业采购经理指数对比正式
     */
    @Override
    public BizManufacturingPmiComparisonFormal selectBizManufacturingPmiComparisonFormalByBaseId(String baseId)
    {
        return bizManufacturingPmiComparisonFormalMapper.selectBizManufacturingPmiComparisonFormalByBaseId(baseId);
    }

    /**
     * 查询山东与全国制造业采购经理指数对比正式列表
     *
     * @param bizManufacturingPmiComparisonFormal 山东与全国制造业采购经理指数对比正式
     * @return 山东与全国制造业采购经理指数对比正式
     */
    @Override
    public List<BizManufacturingPmiComparisonFormal> selectBizManufacturingPmiComparisonFormalList(BizManufacturingPmiComparisonFormal bizManufacturingPmiComparisonFormal)
    {
        return bizManufacturingPmiComparisonFormalMapper.selectBizManufacturingPmiComparisonFormalList(bizManufacturingPmiComparisonFormal);
    }

    /**
     * 新增山东与全国制造业采购经理指数对比正式
     *
     * @param bizManufacturingPmiComparisonFormal 山东与全国制造业采购经理指数对比正式
     * @return 结果
     */
    @Override
    public int insertBizManufacturingPmiComparisonFormal(BizManufacturingPmiComparisonFormal bizManufacturingPmiComparisonFormal)
    {
        return bizManufacturingPmiComparisonFormalMapper.insertBizManufacturingPmiComparisonFormal(bizManufacturingPmiComparisonFormal);
    }

    /**
     * 修改山东与全国制造业采购经理指数对比正式
     *
     * @param bizManufacturingPmiComparisonFormal 山东与全国制造业采购经理指数对比正式
     * @return 结果
     */
    @Override
    public int updateBizManufacturingPmiComparisonFormal(BizManufacturingPmiComparisonFormal bizManufacturingPmiComparisonFormal)
    {
        return bizManufacturingPmiComparisonFormalMapper.updateBizManufacturingPmiComparisonFormal(bizManufacturingPmiComparisonFormal);
    }

    /**
     * 批量删除山东与全国制造业采购经理指数对比正式
     *
     * @param baseIds 需要删除的山东与全国制造业采购经理指数对比正式主键
     * @return 结果
     */
    @Override
    public int deleteBizManufacturingPmiComparisonFormalByBaseIds(String[] baseIds)
    {
        return bizManufacturingPmiComparisonFormalMapper.deleteBizManufacturingPmiComparisonFormalByBaseIds(baseIds);
    }

    /**
     * 删除山东与全国制造业采购经理指数对比正式信息
     *
     * @param baseId 山东与全国制造业采购经理指数对比正式主键
     * @return 结果
     */
    @Override
    public int deleteBizManufacturingPmiComparisonFormalByBaseId(String baseId)
    {
        return bizManufacturingPmiComparisonFormalMapper.deleteBizManufacturingPmiComparisonFormalByBaseId(baseId);
    }

    /**
     * 查询指定日期前一年的数据
     *
     * @param bizManufacturingPmiComparisonRequest 查询的参数
     * @return 查询的结果
     */
    @Override
    public List<BizManufacturingPmiComparisonFormal> getDataOneYearBeforeSpecificDate(BizManufacturingPmiComparisonRequest bizManufacturingPmiComparisonRequest) {
        String specifiedDateStr = bizManufacturingPmiComparisonRequest.getDates();
        /** 根据今年的日期获取上一年的日期 */
        DateTime nextYearDate = DateUtil.offsetMonth(DateUtil.parse(specifiedDateStr, "yyyy年MM月"), -12);
        bizManufacturingPmiComparisonRequest.setNextYearDate(DateUtil.format(nextYearDate,"yyyy-MM-dd"));

        //将原来传入的yyyy年MM月格式转成yyyy-MM-dd格式
        DateTime specifiedDate = DateUtil.parse(specifiedDateStr, "yyyy年MM月");
        bizManufacturingPmiComparisonRequest.setDates(DateUtil.format(specifiedDate,"yyyy-MM-dd"));

        List<BizManufacturingPmiComparisonFormal> result = bizManufacturingPmiComparisonFormalMapper.getDataOneYearBeforeSpecificDate(bizManufacturingPmiComparisonRequest);
        return result;
    }


    /**
     * <p>
     * 查询数据库中数据的日期(year_month字段)并进行去重复操作<br/>
     * 主要用于的是APP端左上角的日期过滤
     * </p>
     *
     * @return 日期的数组
     */
    @Override
    public List<String> getUniqueDatesFromDatabase() {
        List<String> uniqueDatesFromDatabase = bizManufacturingPmiComparisonFormalMapper.getUniqueDatesFromDatabase();
        if(!uniqueDatesFromDatabase.isEmpty()){
            //Mysql中去重关键字和排序不能对同一字段同时使用，这里通过代码来去重
            return uniqueDatesFromDatabase.stream().distinct().collect(Collectors.toList());
        }
        return uniqueDatesFromDatabase;
    }

    /**
     * 查询指定日期前一年的数据图表
     *
     * @param bizManufacturingPmiComparisonRequest 传来的数据
     * @return 查询的结果
     */
    @Override
    public Map<String,Object> getDataOneYearBeforeSpecificDateEcharts(BizManufacturingPmiComparisonRequest bizManufacturingPmiComparisonRequest) {
        String specifiedDateStr = bizManufacturingPmiComparisonRequest.getDates();
        /** 根据今年的日期获取上一年的日期 */
        DateTime nextYearDate = DateUtil.offsetMonth(DateUtil.parse(specifiedDateStr, "yyyy年MM月"), -12);
        bizManufacturingPmiComparisonRequest.setNextYearDate(DateUtil.format(nextYearDate,"yyyy-MM-dd"));

        //将原来传入的yyyy年MM月格式转成yyyy-MM-dd格式
        DateTime specifiedDate = DateUtil.parse(specifiedDateStr, "yyyy年MM月");
        bizManufacturingPmiComparisonRequest.setDates(DateUtil.format(specifiedDate,"yyyy-MM-dd"));

        List<BizManufacturingPmiComparisonFormal> result = bizManufacturingPmiComparisonFormalMapper.getDataOneYearBeforeSpecificDate(bizManufacturingPmiComparisonRequest);

        List<Double> shanDongList = new ArrayList<>();
        List<Double> nationwideList = new ArrayList<>();
        List<String> xList = new ArrayList<>();

        /** 通过这个集合判断是否需要在月上显示年份 */
        Set<String> existYear = new HashSet<>();

        /* 通过这个集合判断是否需要在月上显示年份 */
        List<Double> allDoubleValue = new ArrayList<>();
        for(BizManufacturingPmiComparisonFormal item : result){
            if(StrUtil.isBlank(item.getNationwide()) || StrUtil.isBlank(item.getShandong())){
                throw new RuntimeException(String.format("%s月份中出现全国数据或山东数据为空的情况,请核查",DateUtil.format(item.getYearMonth(),"yyyy年MM月")));
            }
            shanDongList.add(Double.valueOf(item.getShandong()));
            nationwideList.add(Double.valueOf(item.getNationwide()));

            allDoubleValue.add(Double.valueOf(item.getShandong()));
            allDoubleValue.add(Double.valueOf(item.getNationwide()));

            String yyyy = DateUtil.format(item.getYearMonth(), "yyyy");
            if(!existYear.contains(yyyy)){
                existYear.add(yyyy);
                xList.add(DateUtil.format(item.getYearMonth(),"yyyy年MM月"));
            }else{
                xList.add(DateUtil.format(item.getYearMonth(),"MM月"));
            }
        }
        Map<String,Object> resultMap = new HashMap<>(16);
        if(allDoubleValue.isEmpty()){
            resultMap.put("max",0);
            resultMap.put("min",0);
        }else{
            double[] allData = allDoubleValue.stream().mapToDouble(Double::doubleValue).toArray();
            int[] maxAndMin = MaxMinUtil.calculateYAxisRange(allData);
            resultMap.put("max",maxAndMin[0]);
            resultMap.put("min",maxAndMin[1]);
        }
        resultMap.put("shandong",shanDongList);
        resultMap.put("nationwideList",nationwideList);
        resultMap.put("xList",xList);

        return resultMap;
    }

}
